package cn.qiguai.sphapi.controller;

import cn.qiguai.sphapi.controller.response.Status;
import cn.qiguai.sphapi.controller.response.Result;
import cn.qiguai.sphapi.exception.BusinessException;
import cn.qiguai.sphapi.exception.SystemException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;

@RestControllerAdvice
public class ProjectExceptionAdvice {
	/**
	 * 处理参数异常
	 * @param e 异常
	 * @return re
	 */
	@ExceptionHandler({BindException.class, ValidationException.class})
	public Result doParamException(Exception e) {
		e.printStackTrace();

		return new Result(Status.PARAM_ERR);
	}

	/**
	 * 处理业务异常
	 * @param e BusinessException对象
	 * @return result
	 */
	@ExceptionHandler(BusinessException.class)
	public Result doBusinessException(BusinessException e) {
		Result re = new Result();

		e.printStackTrace();
		re.setCode(Status.BUSINESS_ERR.getCode());
		re.setMsg(e.getMessage());

		return re;
	}

	/**
	 * 处理系统异常
	 * @param e SystemException对象
	 * @return result
	 */
	@ExceptionHandler(SystemException.class)
	public Result doSystemException(SystemException e) {
		Result re = new Result();

		e.printStackTrace();
		re.setCode(Status.SYSTEM_ERR.getCode());
		re.setMsg(e.getMessage());

		return re;
	}

	/**
	 * 处理未知异常
	 * @param e Exception
	 * @return result
	 */
	@ExceptionHandler(Exception.class)
	public Result doException(Exception e) {
		Result re = new Result();

		e.printStackTrace();
		re.setCode(Status.UNKNOWN_ERR.getCode());
		re.setMsg("未知异常");

		return re;
	}
}
